package com.keste.adf.ui.bean;


import com.keste.adf.ui.fwk.util.ADFUtils;
import com.keste.adf.ui.fwk.util.QuizUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.share.ADFContext;
import oracle.adf.view.rich.component.rich.fragment.RichDeclarativeComponent;

import oracle.binding.OperationBinding;

import oracle.jbo.ApplicationModule;
import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.domain.Number;
import oracle.jbo.server.ViewObjectImpl;


public class QuestionADayBean {

    private RichDeclarativeComponent questionComponent;
    
    private HashMap questionTypeMap = new HashMap();
    
    private final Integer SUBJECT_ADF=1;
    private final Integer SUBJECT_SOA=2;
    
    private final Number EXAM_QAD_ADF = new Number(1);
    private final Number EXAM_QAD_SOA = new Number(2);;
    
    private Integer CurrentSelectedSubject = SUBJECT_ADF;
    private Number CurrentSelectedExam = EXAM_QAD_ADF;

    public QuestionADayBean() {
 
        questionTypeMap.put(1, "Q_S_Choice");
        questionTypeMap.put(2, "Q_M_Choice");
        questionTypeMap.put(3, "Q_B_Choice");
 
        initQuestionADay(SUBJECT_ADF);
        QuizUtils.getPageFlowScopeMap().put("QExamId", EXAM_QAD_ADF);
    }

    public void initQuestionADay(Integer iSubjectCode)
    {
        try{
            OperationBinding qadMethod = ADFUtils.findOperationBinding("initializeQuestionADay");
            if (qadMethod != null) {
                qadMethod.getParamsMap().put("subjectId", iSubjectCode);
                qadMethod.execute();
            }
            ApplicationModule onlineQuizAM = QuizUtils.getApplicationModuleForDataControl("OnlineQuizAMDataControl");
           
            ViewObjectImpl QuestionADayVO = (ViewObjectImpl)onlineQuizAM.findViewObject("QuestionADayVO");
            
            HashMap questionMap = new HashMap();
            questionMap.put(QuestionADayVO.getRowAtRangeIndex(0).getAttribute("QuestionId"), QuestionADayVO.getRowAtRangeIndex(0).getAttribute("QuestionsText"));
            QuizUtils.getPageFlowScopeMap().put("QuestionMap", questionMap);
            
            String qType = (String)QuestionADayVO.getRowAtRangeIndex(0).getAttribute("QuestionType");
            String type = (String)questionTypeMap.get(Integer.parseInt(qType));//.get[Integer.parseInt(qType)];
            QuizUtils.getPageFlowScopeMap().put("QType", type);

            Map qOptions = new HashMap();
            
            ViewObjectImpl QADAnswersVO = (ViewObjectImpl)onlineQuizAM.findViewObject("QADAnswersVO"); 
            RowSetIterator rsIterator = QADAnswersVO.createRowSetIterator(null);  
            rsIterator.reset();  
            while (rsIterator.hasNext()) {  
                Row rAnswer = rsIterator.next();  
                qOptions.put(rAnswer.getAttribute("AnswerId"), rAnswer);
            }  
            rsIterator.closeRowSetIterator();  

            QuizUtils.getPageFlowScopeMap().put("QOptions", qOptions);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return;
    }

    public void onSubmitQuestion(ActionEvent actionEvent) {
        System.out.println("in doSubmit");
        List answers = (List)ADFContext.getCurrent().getViewScope().get("SelectedAnswers");
        
        
        OperationBinding qadMethod = ADFUtils.findOperationBinding("submitAnswers");
        if (qadMethod != null) {
            qadMethod.getParamsMap().put("qadAnswers", answers);
            qadMethod.execute();
        }
        
        
        System.out.println(answers != null ? answers.size() : null);
    }

    public void OnSubjectSelection(ActionEvent actionEvent) {
        
   //     ApplicationModule onlineQuizAM = QuizUtils.getApplicationModuleForDataControl("OnlineQuizAMDataControl");

        String subjectId = QuizUtils.getPageFlowScopeMap().get("SubjectId").toString();
        Integer iSubjectId = Integer.parseInt(subjectId);

        if(CurrentSelectedSubject != iSubjectId)
        {
            initQuestionADay(iSubjectId);
            ADFUtils.addPartialTarget(this.getQuestionComponent());
            CurrentSelectedSubject = iSubjectId;
            if(iSubjectId == SUBJECT_SOA)
                CurrentSelectedExam = EXAM_QAD_SOA;
            else
                CurrentSelectedExam = EXAM_QAD_ADF;
            
            QuizUtils.getPageFlowScopeMap().put("ExamId", CurrentSelectedExam);
        }
    }

    public void setQuestionComponent(RichDeclarativeComponent questionComponent) {
        this.questionComponent = questionComponent;
    }

    public RichDeclarativeComponent getQuestionComponent() {
        return questionComponent;
    }
    
    private Object getValueObject(String expr, Class returnType){
         FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elctx = fc.getELContext();
        ExpressionFactory elFactory =
        fc.getApplication().getExpressionFactory();
        ValueExpression valueExpr =
        elFactory.createValueExpression(elctx,expr,returnType);
        return valueExpr.getValue(elctx);

    }

//    public Boolean IsQuestionSubmittable(Number examId, Number questionId) {
//        OperationBinding isSubmittableMethod = ADFUtils.findOperationBinding("canUserSubmitThisQuestion");
//        if (isSubmittableMethod != null) {
//            isSubmittableMethod.getParamsMap().put("examId", examId);
//            isSubmittableMethod.getParamsMap().put("questionId", questionId);
//            isSubmittableMethod.execute();
//            
//            Boolean isSubmittable = (Boolean)isSubmittableMethod.getResult();
//            
//            return isSubmittable;
//        }
//        
//        return false;
//    }
}
